// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	/**
	 * searchData方法描述
	 * @param {object} params 参数
	 * @returns {object} 返回值描述
	 */
	async searchData(params) {
		// 参数校验，如无参数则不需要
		if (!params) {
			return {
				errCode: 'PARAMS_IS_NULL',
				errMsg: '参数不能为空'
			}
		}
		const {
			content,
			pageCurrent = 1,
			pageSize = 30
		} = params || {}
		if (!content) {
			return {
				errCode: 'CONTENT_IS_NULL',
				errMsg: 'CONTENT不能为空'
			}
		}
		if (pageSize > 100) {
			// throw new Error('单页数据不可超过100条')
			return {
				errCode: 'PAGESIZE_IS_OVER100',
				errMsg: '单页数据不可超过100条'
			}
		}
		try {
			const db = uniCloud.databaseForJQL({
				clientInfo: this.getClientInfo()
			})
			const regExp = new RegExp(content.replace(/([*.?+$^(){}|\\/])/g, '\\$1'), 'i')
			return await db
				.collection('opendb-mall-goods')
				.where(db.command.or({
					name: regExp
				}, {
					goods_sn: regExp
				}, {
					keywords: regExp
				}))
				.skip((pageCurrent - 1) * pageSize)
				.limit(pageSize)
				.get({
					getCount: true
				})
		} catch (err) {
			return {
				errCode: 'ERROR',
				errMsg: '错误',
				errInfo: err
			}
		}
	},
	/**
	 * getHistoryData方法描述
	 * @returns {object} 返回值描述
	 */
	async getHistoryData() {
		try {
			const clientInfo = this.getClientInfo()
			const {
				deviceId
			} = clientInfo
			if (!deviceId) {
				return {
					errCode: 'DEVICE_ID_IS_NULL',
					errMsg: 'deviceId为空',
				}
			}

			const db = uniCloud.databaseForJQL({
				clientInfo
			})
			return await db
				.collection('custom-search-history')
				.where({
					u_id: deviceId,
					is_delete: 0
				})
				.orderBy('use_count', 'desc')
				.limit(20)
				.get()
		} catch (err) {
			return {
				errCode: 'ERROR',
				errMsg: '错误',
				errInfo: err
			}
		}
	},
	/**
	 * setHistoryData方法描述
	 * @param {string} name 搜索值
	 * @returns {object} 返回值描述
	 */
	async setHistoryData(name) {
		try {
			const clientInfo = this.getClientInfo()
			const {
				deviceId
			} = clientInfo
			if (!deviceId) {
				return {
					errCode: 'DEVICE_ID_IS_NULL',
					errMsg: 'deviceId为空',
				}
			}

			const db = uniCloud.databaseForJQL({
				clientInfo
			})
			// 先查找是否有相同的历史搜索词
			const getDataRes = await db.collection('custom-search-history').where({
				name,
				u_id: deviceId,
				is_delete: 0
			}).get()

			if (Array.isArray(getDataRes?.data) && getDataRes.data.length) {
				// 若有，则不新增数据，更新历史数据使用次数use_count
				const oldDB = uniCloud.database()
				return await oldDB
					.collection('custom-search-history')
					.doc(getDataRes.data[0]?._id)
					.update({
						use_count: oldDB.command.inc(1)
					})
			} else {
				// 若无，则新增数据
				return await db
					.collection('custom-search-history')
					.add({
						name,
						u_id: deviceId
					})
			}
		} catch (err) {
			return {
				errCode: 'ERROR',
				errMsg: '错误',
				errInfo: err
			}
		}
	},
	/**
	 * updateHisCount方法描述
	 * @param {string} _id 数据id
	 * @returns {object} 返回值描述
	 */
	async updateHisCount(_id) {
		if (!_id) {
			return {
				errCode: 'ID_IS_NULL',
				errMsg: '_id为空',
			}
		}
		try {
			const db = uniCloud.database()
			return await db
				.collection('custom-search-history')
				.doc(_id)
				.update({
					use_count: db.command.inc(1)
				})
		} catch (err) {
			return {
				errCode: 'ERROR',
				errMsg: '错误',
				errInfo: err
			}
		}
	},
	/**
	 * deleteHistoryData方法描述
	 * @returns {object} 返回值描述
	 */
	async deleteHistoryData() {
		try {
			const clientInfo = this.getClientInfo()
			const {
				deviceId
			} = clientInfo
			if (!deviceId) {
				return {
					errCode: 'DEVICE_ID_IS_NULL',
					errMsg: 'deviceId为空',
				}
			}

			const db = uniCloud.databaseForJQL({
				clientInfo
			})
			return await db
				.collection('custom-search-history')
				.where({
					u_id: deviceId
				})
				.update({
					is_delete: 1
				})
		} catch (err) {
			return {
				errCode: 'ERROR',
				errMsg: '错误',
				errInfo: err
			}
		}
	}
}